#include "MortSystem.h"

MortSystem::MortSystem()
{
    //ctor
}

MortSystem::~MortSystem()
{
    //dtor
}

static SkipfishConfigData MortSystem::LoadProfileFile( wxString location )
{
    SkipfishConfigData config;

    wxXmlDocument *profile = new wxXmlDocument();

    profile->Load( location );

    wxXmlNode *root = profile->GetRoot();
    wxXmlNode *tool;
    wxXmlNode *section;
    wxXmlNode *child;

    tool = root->GetChildren();

    while ( tool )
    {
        if ( tool->GetName() == wxT("Skipfish") )
        {
            // Load skipfish values
            section = tool->GetChildren();

            while( section )
            {

                // Load children and write to concrete widgets
                child = section->GetChildren();
                wxString s;

                while ( child )
                {

                    if ( child->GetName() == wxT("Username") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.username = s;
                    }
                    else if ( child->GetName() == wxT("Password") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.password = s;
                    }
                    else if ( child->GetName() == wxT("CustomCookie") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.customCookie = s;
                    }
                    else if ( child->GetName() == wxT("MaxDepth") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.maxDepth = wxAtoi( s );
                    }
                    else if ( child->GetName() == wxT("MaxTotalRequests") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.maxTotalRequest = wxAtoi( s );
                    }
                    else if ( child->GetName() == wxT("DontDescendInto5xxLocations") )
                    {
                        // The value must be a boolean so the program convert an integer value 0/1 to false/true value
                        child->GetAttribute(wxT("Value"), &s);
                        long v;
                        v = wxAtol( s );
                        bool value;

                        if ( v == 0 )
                            value = false;
                        else
                            value = true;

                        config.dontDescendInto5xxLocations = value;
                    }
                    else if ( child->GetName() == wxT("WriteOutputTo") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.writeOutputTo = s;
                    }
                    else if ( child->GetName() == wxT("LogWarningsAboutMixedContent") )
                    {
                        // The value must be a boolean so the program convert an integer value 0/1 to false/true value
                        child->GetAttribute(wxT("Value"), &s);
                        long v;
                        v = wxAtol( s );
                        bool value;

                        if ( v == 0 )
                            value = false;
                        else
                            value = true;

                        config.logWarningsAboutMixedContent = value;
                    }
                    else if ( child->GetName() == wxT("LoadWordlist") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.loadWordlist = s;
                    }
                    else if ( child->GetName() == wxT("DontLearnNewWordsForTheSite") )
                    {
                        // The value must be a boolean so the program convert an integer value 0/1 to false/true value
                        child->GetAttribute(wxT("Value"), &s);
                        long v;
                        v = wxAtol( s );
                        bool value;

                        if ( v == 0 )
                            value = false;
                        else
                            value = true;

                        config.dontLearnNewWordForTheSite = value;
                    }
                    else if ( child->GetName() == wxT("DontUpdateWordlistBasedOnTheScan") )
                    {
                        // The value must be a boolean so the program convert an integer value 0/1 to false/true value
                        child->GetAttribute(wxT("Value"), &s);
                        long v;
                        v = wxAtol( s );
                        bool value;

                        if ( v == 0 )
                            value = false;
                        else
                            value = true;

                        config.dontUpdateWordlistBaseOnTheScan = value;
                    }
                    else if ( child->GetName() == wxT("MaxGlobalTCPConnections") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.maxGlobalTCPConnections = wxAtoi( s );
                    }
                    else if ( child->GetName() == wxT("MaxHTTPFails") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.maxCosecutiveHttpFails = wxAtoi( s );
                    }
                    else if ( child->GetName() == wxT("TotalRequestTimeout") )
                    {
                        child->GetAttribute(wxT("Value"), &s );
                        config.totalRequestTimeout = wxAtoi( s );
                    }


                    // Load the next value if any otherwise child = NULL and exit while loop
                    child = child->GetNext();
                }

                // Load next section if any otherwise section = NULL and exit while loop
                section = section->GetNext();
            }
        }

        // Load next tool config if any otherwise tool = NULL and exit while loop
        tool = tool->GetNext();
    }

    return config;
}
